package com.back.service.impl;

import com.back.model.dto.ExamsQueryDTO;
import com.back.model.entity.TaskInfo;
import com.back.model.reslut.PageData;
import com.back.model.vo.TaskInfoVO;
import com.back.service.ExamsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.back.model.entity.Exams;
import com.back.mapper.ExamsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Administrator
* @description 针对表【exams(考试表)】的数据库操作Service实现
* @createDate 2025-08-21 17:00:00
*/
@Service
public class ExamsServiceImpl extends ServiceImpl<ExamsMapper, Exams>
    implements ExamsService {



    @Override
    public Exams getExams(ExamsQueryDTO examsQueryDTO) {
        //返回考试
        LambdaQueryWrapper<Exams> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Exams::getIsActive,examsQueryDTO.getIsActive());
        queryWrapper.eq(Exams::getBelongId,examsQueryDTO.getBelongId());
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public  PageData<Exams> getExamsList(ExamsQueryDTO examsQueryDTO) {

        List<Exams> TaskVOS = new ArrayList<>();
        PageData<Exams> pageData = new PageData<>();

        LambdaQueryWrapper<Exams> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Exams::getIsActive,examsQueryDTO.getIsActive());
        queryWrapper.eq(Exams::getBelongId,examsQueryDTO.getBelongId());


        Page<Exams> page = new Page<>(examsQueryDTO.getPageNum(), examsQueryDTO.getPageSize());
        IPage<Exams> listPage = baseMapper.selectPage(page,queryWrapper);

        listPage.getRecords().forEach(item -> {
            Exams exams = new Exams();
            BeanUtils.copyProperties(item,exams);
            TaskVOS.add(exams);
        });
        pageData.setListData(TaskVOS);
        pageData.setPageCurrent(listPage.getCurrent());
        pageData.setPageSize(listPage.getSize());
        pageData.setTotal(listPage.getTotal());
        return pageData;
    }

    @Override
    public Exams createExams(Exams exams) {
        if (exams.getId()!=null) {
            updateById(exams);
        }else {
           save(exams);
        }
        return null;
    }

    @Override
    public void pushExams(Integer examsId) {
        Exams exams = new Exams();
        exams.setId(examsId);
        exams.setIsActive(1);
        updateById(exams);
    }
}




